package cn.com.gensoft.app.web.user.controller;

import cn.com.gensoft.app.core.utils.PasswordUtil;
import cn.com.gensoft.app.web.user.dto.SysUserDTO;
import cn.com.gensoft.app.web.user.entity.SysUserEntity;
import cn.com.gensoft.app.web.user.mapStruct.SysUserMapStruct;
import cn.com.gensoft.app.web.user.service.SysUserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.codahale.metrics.annotation.Timed;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

/**
 * 控制层 示例  demo //: todo 待完善
 * 2018-08-03 he
 */
@Api("用户资源操作接口")
@RestController
@Slf4j
@RequestMapping("/api")
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysUserMapStruct sysUserMapStruct;

	/**
	 * 用户登录
	 */
	//@SysLog("登录")
	@Timed
	@ApiOperation(value = "用户登陆", notes = "用户登陆")
	@PostMapping(value = "/users/login")
	public ResponseEntity<String> login(String username, String password, HttpSession session) {
		log.info("用户登陆:{}",username);
		UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(username,password);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(usernamePasswordToken);
			session.setAttribute("user", subject.getPrincipal());
			return ResponseEntity.ok().body("success");
		}catch (UnknownAccountException e) {
			e.printStackTrace();
			return ResponseEntity.badRequest().body("用户名错误");
		} catch (IncorrectCredentialsException e) {
			e.printStackTrace();
			return ResponseEntity.badRequest().body("密码错误");
		}  catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.badRequest().body("登录失败");
		}
	}

	/**
	 * 查询操作 GetMapping
	 * @return
	 */
	@Timed
	@GetMapping("/users")
	public ResponseEntity<Page<SysUserEntity>> listUser(SysUserDTO sysUserDTO, Page<SysUserEntity> page) {
    SysUserEntity userEntity = sysUserMapStruct.toEntity(sysUserDTO);
    page = sysUserService.selectPage(page, new EntityWrapper<>(userEntity));
    return new ResponseEntity(page, HttpStatus.OK);

  }

	/**
	 * 添加操作 PostMapping
	 * @param sysUserDTO
	 * @return
	 */
	@Timed
	@PostMapping("/users")
	public ResponseEntity<Boolean>  addUser(@RequestBody @Valid SysUserDTO sysUserDTO){
		SysUserEntity sysUserEntity = sysUserMapStruct.toEntity(sysUserDTO);
		Map<String,String> map = PasswordUtil.encryptPassword(sysUserEntity.getUsername(),sysUserEntity.getPassword());
 		sysUserEntity.setPassword(map.get("password"));
		sysUserEntity.setSalt(map.get("salt"));
		return ResponseEntity.ok().body(sysUserService.insert(sysUserEntity));
	}

	/**
	 * 修改操作 PutMapping
	 * @param sysUserDTO
	 * @return
	 */
	@Timed
	@PutMapping("/users")
	public ResponseEntity<Boolean>  updateUser(@RequestBody @Valid SysUserDTO sysUserDTO){
		SysUserEntity sysUserEntity = sysUserMapStruct.toEntity(sysUserDTO);
		return ResponseEntity.ok().body(sysUserService.updateById(sysUserEntity));
	}

	/**
	 * 删除操作 DeleteMapping
	 * @return
	 */
	@Timed
	@DeleteMapping("/users/{id}")
	public ResponseEntity<Void>  delUser(@PathVariable String id){
		sysUserService.deleteById(id);
		return ResponseEntity.noContent().build();
	}

}
